const mysql = require('mysql2');
const localStorage = require('../../localStorage');
const CONSTS = require('../../consts');

let connectionConfig = localStorage.getItem(CONSTS.DB_CONFIG);
connectionConfig = Object.assign(CONSTS.DEFAULT_DB_CONFIG, connectionConfig);
const pool = mysql.createPool(connectionConfig);

const mysqlClient = {
    query: function (sql, params, callback) {
        pool.query(sql, params, function (err, results, fields) {
            if (err) {
                console.log(err);
                callback(err, null);
                return;
            }
            callback(null, results);
        });
    }
}

module.exports = mysqlClient;